﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Advisee_Notes : System.Web.UI.Page
{
    //////////////////
    //Page Functions//
    //////////////////

    /// <summary>
    /// Page_Load()
    /// 
    /// Initializes the page.
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //Initialize main box to empty
        NotesList.Text = "";
        //Get the notes in the database and display them in the notes area
        List<Dictionary<String, Object>> results = StoredProcedures.getNotes(SharedFunctions.getAdviseeID(Request), SharedFunctions.generateKey(SharedFunctions.getAdviseeID(Request)));
        foreach(Dictionary<String, Object> i in results)
        {
            //Take off the timestamp (just want the date)
            String date = i["date"].ToString();
            date = date.Substring(0, date.Length - 12);
            NotesList.Text += i["note"] + " - "+i["author"] + ", " + date + System.Environment.NewLine;
        }
    } //end Page_Load()

    /// <summary>
    /// AddButton_Click()
    /// 
    /// Called when user clicks the add button.
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void AddButton_Click(object sender, EventArgs e)
    {
        //Get the relevant information for the note
        String note = NoteInputText.Text;
        String author =  Session["Advisee_firstName"] + " " + Session["Advisee_lastName"];
        //Add the note to the database
        //Check that the note actually has text
        if (NoteInputText.Text == "")
        {
            MessageBox.Show("Add text to your note before you add it!");
            return;
        }
        String adviseeID = SharedFunctions.getAdviseeID(Request);
        if (StoredProcedures.addNote(adviseeID, note, author, SharedFunctions.generateKey(adviseeID)) == StoredProcedures.FAILURE)
        {
            MessageBox.Show("Could not add note");
            return;
        }
       
        NoteInputText.Text = "";
        Page_Load(sender, e);
    } //end AddButton_Click()

    /// <summary>
    /// CloseButton_Click()
    /// 
    /// Called when user clicks the close button.
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void CloseButton_Click(object sender, EventArgs e)
    {
        //Redirect to default
        Response.Redirect("~/Advisee/Default.aspx");
    } //end CloseButton_Click()

    /// <summary>
    /// HelpButton_Click()
    /// 
    /// Called when user clicks the help button.
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void HelpButton_Click(object sender, EventArgs e)
    {
        //Redirect to help
        Response.Redirect("~/Advisee/Help.aspx");
    } //end HelpButton_Click()




}